package com.hujiheng.paike.controller;

import cn.hutool.core.map.MapUtil;
import com.hujiheng.paike.common.lang.Result;
import com.hujiheng.paike.pojo.ClassInfo;
import com.hujiheng.paike.pojo.Student;
import com.hujiheng.paike.pojo.Teacher;
import com.hujiheng.paike.service.ClassInfoService;
import com.hujiheng.paike.service.StudentService;
import com.hujiheng.paike.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/class")
public class ClassController {
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;

    @RequestMapping("/list")
    public Result list() {
        List<ClassInfo> classInfoList = classInfoService.getClassInfoList();
        return Result.success(classInfoList);
    }

    @RequestMapping("/getClassInfo/{id}")
    public Result getClassInfo(@PathVariable("id") int classId) {
        ClassInfo classInfoById = classInfoService.getClassInfoById(classId);
        Teacher byId = teacherService.getById(classInfoById.getTeacherId());
        List<Student> studentsByClassId = studentService.getStudentsByClassId(classId);
        return Result.success(MapUtil.builder()
                .put("classId", classInfoById.getClassId())
                .put("className", classInfoById.getClassName())
                .put("classLocal", classInfoById.getClassLocal())
                .put("teacher", byId)
                .put("students", studentsByClassId)
                .map());
    }

    @RequestMapping("/add")
    public Result add(@Validated @RequestBody ClassInfo classInfo) {
        int add = classInfoService.add(classInfo);
        if (add < 1) {
            return Result.fail("添加失败");
        }
        return Result.success("添加成功");
    }

}
